package com.gongpingjialoan.activity.car;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.SelectAddressActivity;
import com.gongpingjialoan.activity.sell.SellCarStatusActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.utility.UserLocation;
import com.gongpingjialoan.widget.DateTimePickDailog;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/5/28.
 */
public class SellCarAddressActivity extends BaseActivity implements View.OnClickListener {

    private static final int GETADDRESS = 10;

    private TextView timeTextView, addressTextView, tgTextView;

    private Button sureButton;

    private DateTimePickDailog datePickerDialog;

    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    private String data = "";

    private NetDataJson mNetDataJson;

    private String address = "", car_id = "";

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "sell_submit_validate";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_layout);
        setTitle("快速卖车");
        car_id = getIntent().getStringExtra("car_id");
        tgTextView = (TextView) findViewById(R.id.right);
        loadingDialog = new LoadingDialog(this);
        datePickerDialog = new DateTimePickDailog(this, 30, null);
        timeTextView = (TextView) findViewById(R.id.time);
        addressTextView = (TextView) findViewById(R.id.address);
        sureButton = (Button) findViewById(R.id.sure);
        address = UserLocation.getInstance().getAddress();
        addressTextView.setText(address);
        timeTextView.setOnClickListener(this);
        addressTextView.setOnClickListener(this);
        sureButton.setOnClickListener(this);
        tgTextView.setOnClickListener(this);
        tgTextView.setVisibility(View.VISIBLE);

    }


    public void setTime(String str, Date date, String time) {
        String newTime = time.replace("点", "");
        data = df.format(date) + " " + newTime + ":00";

        int t = Integer.parseInt(newTime);
        String addTime = "";
        if (t + 2 < 24) {
            addTime = " - " + (t + 2) + "点";
        } else if (t + 2 == 24) {
            addTime = " - 0点";
        } else {
            addTime = " - 1点";
        }
        timeTextView.setText(str + addTime);
    }

    @Override
    public void onClick(View v) {
        if (v == timeTextView) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentSelectTime);
            datePickerDialog.show();
        } else if (v == addressTextView) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentLocation);
            startActivityForResult(new Intent(this, SelectAddressActivity.class), GETADDRESS);
        } else if (v == sureButton) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentCommit);
            if (TextUtils.isEmpty(data) || TextUtils.isEmpty(address)) {
                PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentAlert);
                AlertDialog.Builder ab = new AlertDialog.Builder(this);
                ab.setTitle("提示");
                ab.setMessage("您还没有填写服务时间或者看车地点, 是否跳过？");
                ab.setNegativeButton("继续填写", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentAlertHome);
                    }
                });
                ab.setPositiveButton("跳过", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentAlertSkip);
                        StepMonitor.getInstance().addMonitor(new Monitor("jump", "sell_submit_validate", System.currentTimeMillis(), 1f));
                        startActivity(new Intent(SellCarAddressActivity.this, SellCarStatusActivity.class));
                        finish();
                    }
                });
                ab.create().show();
                return;
            } else {
                postAddress();
            }
        } else if (v == tgTextView) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.SellCarAppointmentSkip);
            StepMonitor.getInstance().addMonitor(new Monitor("jump", "sell_submit_validate", System.currentTimeMillis(), 1f));
            startActivity(new Intent(SellCarAddressActivity.this, SellCarStatusActivity.class));
            finish();
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == GETADDRESS) {
                address = data.getExtras().getString("address_title") + data.getExtras().getString("address_detail");
                //  Toast.makeText(this, data.getExtras().getString("address_title"), Toast.LENGTH_SHORT).show();
                addressTextView.setText(address);
            }
        }

    }

    private void postAddress() {
        StepMonitor.getInstance().addMonitor(new Monitor("click" , "sell_submit_validate" , System.currentTimeMillis() , 1f));
        if (mNetDataJson == null) {
            mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    loadingDialog.dismiss();
                    Toast.makeText(SellCarAddressActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDataJsonUpdate(JSONObject json) {
                    loadingDialog.dismiss();
                    startActivity(new Intent(SellCarAddressActivity.this, SellCarStatusActivity.class));
                    finish();
                }
            });
        }
        mNetDataJson.setUrl(API.appointment_address);
        mNetDataJson.addParam("begin_time", data);
        mNetDataJson.addParam("address", address);
        mNetDataJson.addParam("car_id", car_id);
        mNetDataJson.request("post");
        loadingDialog.show();
    }

}
